package com.apartment.control;

import com.apartment.data.entity.Admin;
import com.apartment.data.entity.User;
import com.apartment.sevice.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;

import java.util.List;

/**
 * Author : xuzhiyong
 * Date : 2022/5/30 11:38
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "admin/adminAdd";
    }

    @RequestMapping("/addAdmin")
    public String add(Admin admin){
        adminService.addAdmin(admin);
        return "redirect:/admin/adminList";
    }

    @RequestMapping("/toEdit")
    public String toEdit(Model model, Integer id) {
        Admin admin = adminService.findAdminById(id);
        model.addAttribute("admin",admin);
        return "admin/adminEdit";
    }

    @RequestMapping("/editAdmin")
    public String edit(Admin admin){
        adminService.updateAdmin(admin);
        return "redirect:/admin/adminList";
    }

    @RequestMapping("/delete")
    public String delete(int id) {
        adminService.deleteAdminById(id);
        return "redirect:/admin/adminList";
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "admin/adminLogin";
    }

    @RequestMapping("/login")
    public String login(Model model,@RequestParam("username") String username, @RequestParam("password") String password) {
//        Admin admin = adminService.findAdminByUsername(username);
        Admin admins = adminService.findAdminByUsername1(username);
        model.addAttribute("admin",admins);
        if (adminService.adminLogin(username,password)!= null){
            return "admin/adminIndex";
        }else
            return "error";
    }

    @RequestMapping("/adminHomePage")
    public String adminHomePage(Model model,String username){
        Admin admins = adminService.findAdminByUsername1(username);
        model.addAttribute("admin",admins);
        return  "admin/adminHomePage";
    }

    @RequestMapping("/exit")
    public String exit(){
        return "redirect:/admin/toLogin";
    }

    @ResponseBody
    @RequestMapping("/findAdminByUsername")
    public List<Admin> find(@RequestParam("username") String username){
        List<Admin> admins = adminService.findAdminByUsername(username);
        return admins;
    }

    @RequestMapping("/adminList")
    public String findAllAdmin(Model model,String username) {
        Admin admin1 = adminService.findAdminByUsername1(username);
        model.addAttribute("admin1",admin1);
        System.out.println("查询所有的管理员");
        List<Admin> admins = adminService.findAllAdmin();
        model.addAttribute("admins", admins);
        return "admin/adminList";
    }

    @RequestMapping("/toForgetPassword")
    public String toforget(){
        return "admin/adminForgetPassword";
    }

    private static Logger LOG= LoggerFactory.getLogger(UserController.class);

    @Value("${spring.mail.username}")
    private String Mail;

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private TemplateEngine templateEngine;

    @PostMapping("/forgetPassword")
    @ResponseBody
    public String sendMail(@RequestParam("mail") String mail){
        Admin admin = adminService.findAdminByMail(mail);
        if(admin !=null){
            String code = admin.getPassword();
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(Mail);
            message.setTo(mail);
            message.setSubject("你的密码");
            message.setText("密码为："+code);
            mailSender.send(message);
            System.out.println(message);
            LOG.info("发送成功！！！");
            return code;
        }else {
            String code = "";
            return code;
        }
    }
}
